package com.example.book.service.impl.admin;

import com.example.book.context.BaseContext;
import com.example.book.dto.BookDTO;
import com.example.book.dto.BookPageDTO;
import com.example.book.dto.BorrowPageDTO;
import com.example.book.entity.Book;
import com.example.book.entity.Borrow;
import com.example.book.mapper.BookMapper;
import com.example.book.mapper.BorrowMapper;
import com.example.book.mapper.CategoryMapper;
import com.example.book.result.PageResult;
import com.example.book.vo.BookVO;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.security.auth.login.AccountNotFoundException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class BookServiceImpl implements BookService{
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private BorrowMapper borrowMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    //图书的分页查询
    @Override
    public PageResult pageInquire(BookPageDTO bookPageDTO) {
        PageHelper.startPage(bookPageDTO.getPage(), bookPageDTO.getPageSize());//获取页数和记录数
        Page <BookVO> page = bookMapper.page(bookPageDTO);//获取图书信息
        long total = page.getTotal();//获取总记录数
        List<BookVO> bookVOS = page.getResult();//获取记录数的集合
        PageResult pageResult = new PageResult(total,bookVOS);//将集合放入pageResult实体类中
        return pageResult;
    }
    //新增图书
    @Override
    @Transactional
    public void insertBook(BookDTO bookDTO) throws AccountNotFoundException {
        Book book = new Book();
        BeanUtils.copyProperties(bookDTO,book);//将bookDTO的属性赋值给book
        book.setStatus(1);//设置book的status属性
        book.setCreateTime(LocalDateTime.now());
        book.setUpdateTime(LocalDateTime.now());
        Book book1 = bookMapper.getBookByBookName(bookDTO.getBookName());
        //判断是否已经有该书，若书籍类型分配错误则弹出相应的信息，无则新建，反之直接加上要添加的本数
        if(book1==null) {
            bookMapper.insertBook(book);//插入书籍信息
            String name = categoryMapper.getCategory(book.getCategoryId());//查询是否存在未定义的新类型
            if (name==null) throw new AccountNotFoundException("类型出错，没有该类型数据");
        }else if(book1.getCategoryId()!=book.getCategoryId()){
            throw new AccountNotFoundException("类型分配错误");
        }else bookMapper.updateBookMeg(book);
    }
    //批量删除图书
    @Override
    @Transactional
    public void deleteByBookName(List<String> bookName) throws AccountNotFoundException {
        //删除之前要先判断书籍是否已经还完
        List<Borrow> borrows = borrowMapper.getBook(bookName);
        if(borrows.size() == 0){
            //判断已经删完之后在去book库中删除书籍的相关信息
            bookMapper.deleteBookMeg(bookName);
        }else throw new AccountNotFoundException("存在书籍未还");
    }
}
